
// JFC
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.*;
import com.golden.gamedev.object.collision.*;

//import board.*;


class MyGame extends Game {

    MyAnimatedSprite hero;
    Sprite block;
    Board board;

    SpriteGroup  PLAYER;
    SpriteGroup  BLOCKS;

    CollisionManager collisionType;


    public void initResources() {
	//  BufferedImage[] images = getImages("yoda.png",1,1);
	BufferedImage[] images = { getImage("yoda0.png"), 
				   getImage("yoda1.png") };
	
	BufferedImage iblock = getImage("block.png");

	double x = 0;
        double y = 0;

        hero = new MyAnimatedSprite(images, x, y);

	block = new Sprite(iblock, 600, 400);

	hero.setAnimate(true);    // animate the sprite
        hero.setLoopAnim(true);   // animate it continously

	BLOCKS = new SpriteGroup("Blocks");
	BLOCKS.add(block);

	PLAYER = new SpriteGroup("Player");
	PLAYER.add(hero);

	collisionType = new ObjectCollision();
        collisionType.setCollisionGroup(PLAYER, BLOCKS);


	board = new Board(1024, 768);
    }


    public void update(long elapsedTime) {

	if (keyDown(KeyEvent.VK_LEFT))   hero.moveX(-0.1 * elapsedTime);
	if (keyDown(KeyEvent.VK_RIGHT))  hero.moveX(0.1 * elapsedTime);

	if (keyPressed(KeyEvent.VK_ESCAPE)) {
	  finish();
	}

	PLAYER.update(elapsedTime);
        BLOCKS.update(elapsedTime);

	collisionType.checkCollision();

        
    }

    
    public void render(Graphics2D g) {
	board.render(g);

	PLAYER.render(g);
        BLOCKS.render(g);
    }



    public static void main(String[] args) {
        GameLoader game = new GameLoader();
        game.setup(new MyGame(), new Dimension(1024,768), false);
        game.start();

    }
};



class MyAnimatedSprite extends AnimatedSprite {

    public double prevx, prevy;

    public MyAnimatedSprite(BufferedImage[] image, double x, double y)
    {
	super(image, x, y);
	prevx = x;
	prevy = y;
    }

    public void moveX(double x)
    {
	prevx = getX();
	super.moveX(x);
    }


};



class ObjectCollision extends BasicCollisionGroup {

    public ObjectCollision() {
	pixelPerfectCollision = true;
    }

 

    public void collided(Sprite s1, Sprite s2) {
	
      //AnimatedSprite mys2 = (AnimatedSprite)s2;
	//mys2.setX(mys2.prevx);	
	//s2 = (Sprite)mys2;
      
      System.out.println("collided() called");

      System.out.print("s1 is ");
      if(s1 instanceof MyAnimatedSprite) System.out.println("MyAnimatedSprite");
      else System.out.println("Sprite");

      System.out.print("s2 is ");
      if(s2 instanceof MyAnimatedSprite) System.out.println("MyAnimatedSprite");
      else System.out.println("Sprite");

    }
   

};